import { NextResponse } from 'next/server';
import { cars } from '@/data/cars';

export async function GET() {
  try {
    // 模拟API延迟
    await new Promise(resolve => setTimeout(resolve, 150));

    const brands = [...new Set(cars.map(car => car.brand))];
    const newCars = cars.filter(car => car.type === 'new').length;
    const usedCars = cars.filter(car => car.type === 'used').length;
    const avgPrice = cars.reduce((sum, car) => sum + car.price, 0) / cars.length;

    return NextResponse.json({
      code: 200,
      message: 'success',
      data: {
        totalCars: cars.length,
        newCars,
        usedCars,
        brandsCount: brands.length,
        avgPrice: Math.round(avgPrice)
      },
      timestamp: Date.now()
    });
  } catch (error) {
    return NextResponse.json({
      code: 500,
      message: 'Internal server error',
      data: null,
      timestamp: Date.now()
    }, { status: 500 });
  }
}